<?php

namespace Admin\Controller;

use Think\Controller;

class HomeController extends CommonController
{
    //主页
    public function home()
    {
        $total = [];
        //未处理反馈数量
        $where = [];
        $where['status'] = array('eq', '1');
        $feedbackModel = M('user_feedback', '', 'KEFU');
        $feedback_tol = $feedbackModel->where($where)->count();
        $total['feedback'] = $feedback_tol;
        //我处理中的工单
        $where = [];
        $where['status'] = array('in', array(1, 2));
        $where['accept_name'] = array('eq', $_SESSION['name']);
        $work_order = M('work_order', '', 'KEFU');
        $order_tol = $work_order->where($where)->count();
        $total['order'] = $order_tol;
        //需要我处理的调查
        $where = [];
        $where['status'] = array('in', array(0, 7, 1, 8));
        $where['out_kf_id'] = $_SESSION['id'];
        $surve_total = M('anti_check_user', '', 'KEFU')->where($where)->count();
        $total['surve'] = $surve_total;
        //逾期提醒
        $where = [];
        $where['status'] = array('eq', '1');
        $where['merchant_num'] = array('eq', 'dh_kefu');
        $overdue_total = M('user_overdue', '', 'URGE')->where($where)->count();
        $total['overdue'] = $overdue_total;
        //今日呼出
        $today=strtotime(date('Y-m-d',time()));
        $where = [];
        $where['ctime']=array('between',array($today,time()));
        $where['calltype'] = array('in', array(1,4));
        $where['admin'] = array('eq', $_SESSION['name']);
        $call_out=M('call_info','','KEFU')->where($where)->count();

        //今日在线回复  总的回复条数
        $date = strtotime(date('Y-m-d', time()));
        $w = [];
        $w['ctime'] = array('between', array($date, time()));
        $w['vid'] = array('eq', '0');
        $w['admin_name'] = array('eq',$_SESSION['name']);
        $feedback = M('user_message', '', 'KEFU')->where($w)->count();
        //今日接听
        $w = [];
        $w['ctime'] = array('between', array($date, time()));
        $where['calltype'] = array('in', array(2,3));
        $w['admin'] = array('eq', $_SESSION['name']);
        $answer=M('call_info','','KEFU')->where($w)->count();

          //今日任务
        $w = [];
        $w['task_time'] = array('between', array($date, time()));
        $w['accept'] = array('eq', $_SESSION['id']);
        $task=M('task','','KEFU')->where($w)->count();
        //今日工单
        $w = [];
        $w['ctime'] = array('between', array($date, time()));
        $w['accept_name'] = array('eq', $_SESSION['name']);
        $order=M('work_order','','KEFU')->where($w)->count();

        //客服绩效统计
        ///X轴时间
        $timetoday = strtotime(date("Y-m-d", time()));//今天凌晨时间戳
        //本月份
        $month=date('m',time());
        $date = '';
        $month_s = '';
        $out_day_spot = '';
        $out_spot_all='';
        $month_spot='';
        for ($i = 6; $i >= 0; $i--) {
            $time = date("m-d", $timetoday - $i * 24 * 3600);
//            $time_click = date("Y-m-d", $timetoday - $i * 24 * 3600);
            $time_last = strtotime(date("Y-m-d", $timetoday - $i * 24 * 3600));
            $time_next = strtotime(date("Y-m-d", $timetoday - ($i - 1) * 24 * 3600));

            //当天外呼 有效
            $where=[];
            $where['calltype']=array('eq',1);
            $where['state']=array('eq',1);
//            $where['content']=array('exp','is not null');
            $where['ctime']=array('between',array($time_last,$time_next));
            if($_SESSION['role_id']==2){
                $where['admin']=array('eq',$_SESSION['name']);
            }
            $out_num=M('call_info','','KEFU')->where($where)->count();

            //     //当天外呼 全部
            $where=[];
            $where['calltype']=array('eq',1);
            $where['ctime']=array('between',array($time_last,$time_next));
            if($_SESSION['role_id']==2){
                $where['admin']=array('eq',$_SESSION['name']);
            }
            $out_num_all=M('call_info','','KEFU')->where($where)->count();

            //当月外呼
            $m=$month-$i;
            if($m<=0){
                $m=12-abs($m);
            }
            $month_arr=$this->mFristAndLast(date('Y',time()),$m);
            $where=[];
            $where['calltype']=array('eq',1);
            $where['ctime']=array('between',array($month_arr['firstday'],$month_arr['lastday']));
            if($_SESSION['role_id']==2){
                $where['admin']=array('eq',$_SESSION['name']);
            }
            $month_out=M('call_info','','KEFU')->where($where)->count();
            if($i==6){
                $date = $time;
                $month_s=$m;
                $out_day_spot=$out_num;
                $out_spot_all=$out_num_all;
                $month_spot=$month_out;
            }else{
                $date = $date . ',' . $time;
                $month_s = $month_s . ',' . $m;
                $out_day_spot = $out_day_spot . ',' . $out_num;
                $out_spot_all=$out_spot_all.','.$out_num_all;
                $month_spot=$month_spot.','.$month_out;
            }
        }
//        //今日放款额度
//        $w = [];
//        $w['get_time'] = array('between', array($date, time()));
//        $borrowed = M('user_borrow', '', 'CAIWU')->where($w)->sum('money');
//        //今日还款   部分还款表与还款表的关联，
//        $w = [];
//        $w['real_time'] = array('between', array($date, time()));
//        $repay = M('user_repayment', '', 'CAIWU')->where($w)->field('repay_money,repayment_no')->select();//已还款
//        $repay_count=array_sum(array_column($repay,'repay_money'));
//        $repayment_no = array_column($repay, 'repayment_no');
//
//        $w = [];
//        $w['real_time'] = array('between', array($date, time()));
//        $w['repayment_no'] = array('notin', $repayment_no);
//        $w['status'] = array('eq', 2);
//        $part_repay = M('part_repayment','','CAIWU')->where($w)->sum('money');//部分还款
//        $today_pay=$part_repay+$repay_count;
//
//        //今日逾期还款
//        $w = [];
//        $w['real_time'] = array('between', array($date, time()));
//        $w['is_overdue'] = array('eq', 2);
//        $repay = M('user_repayment', '', 'CAIWU')->where($w)->field('repay_money,repayment_no')->select();//已还款
//        $repay_count=array_sum(array_column($repay,'repay_money'));
//        $repayment_no = array_column($repay, 'repayment_no');
        //
//        $w = [];
//        $w['real_time'] = array('between', array($date, time()));
//        $w['repayment_no'] = array('notin', $repayment_no);
//        $w['status'] = array('eq', 2);
//        $w['type'] = array('eq', 2);
//        $part_repay = M('part_repayment','','CAIWU')->where($w)->sum('money');//部分还款
//        $overdue_pay=$part_repay+$repay_count;

        $this->assign(array(
            'total' => $total,
            'feedback' => $feedback,
            'call_out' => $call_out,
            'answer' => $answer,
            'task' => $task,
            'order' => $order,
            'times' => $date,
            'months' => $month_s,
            'out_day_spot' => $out_day_spot,
            'out_spot_all' => $out_spot_all,
            'out_month_spot' => $month_spot,
        ));
        $this->display();
    }


    /**
     * 获取指定月份的第一天开始和最后一天结束的时间戳
     *
     * @param int $y 年份 $m 月份
     * @return array(本月开始时间，本月结束时间)
     */
    function mFristAndLast($y="",$m=""){
        if($y=="") $y=date("Y");
        if($m=="") $m=date("m");
        $m=sprintf("%02d",intval($m));
        $y=str_pad(intval($y),4,"0",STR_PAD_RIGHT);
        $m>12||$m<1?$m=1:$m=$m;
        $firstday=strtotime($y.$m."01000000");
        $firstdaystr=date("Y-m-01",$firstday);
        $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
        return array("firstday"=>$firstday,"lastday"=>$lastday);
    }

}